global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A35_middleskillwage.log, replace name(tabl)

capture noi { 

	qui do ${code_dir}/config/tabletools.do
	use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear
	tab year, gen(YY_)
	estimates clear

	clonevar stockown = k${depvar}_${ttt} 
	clonevar stockownzero = k${depvar}_${ttt}0  
	clonevar stockother = kNOT_${depvar}_${ttt} 
	clonevar stockotherzero = kNOT_${depvar}_${ttt}0

	clonevar spilloversown = spill${depvar}${ttt}_1995_a
	clonevar spilloversother = spillN${depvar}${ttt}_1995_a
	clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
	clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0

	clonevar LSW = lswMPm_1995_a
	clonevar MSW = mswMPm_1995_a
	clonevar HSW = hswMPm_1995_a

	clonevar VAEMP = vaempMPm_1995_a
	clonevar GDPGAP = lngdpgap_1995_a
	clonevar GDPPC = gdppcMPm_1995_a

	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+2 & year <= 2009+2
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995
	egen yearctry = group(year country_shr_1995) if year <= 2009
	egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009
	egen yearindustry = group(year industry) if year <= 2009
	sort lse_id year    

	*1) LSW and MSW | F + IY
	ppmlhdfe F2.${depvar}_${ttt} LSW MSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	hasvar stock* spill*, local(stockspill)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_1

	*2) HSW and MSW | F + IY
	ppmlhdfe F2.${depvar}_${ttt} MSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	hasvar stock* spill*, local(stockspill)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_2

	*3) LSW, HSW and MSW | F + IY
	ppmlhdfe F2.${depvar}_${ttt} LSW MSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	hasvar stock* spill*, local(stockspill)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_3

	*4) LSW and MSW | F + IY + CY
	ppmlhdfe F2.${depvar}_${ttt} LSW MSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	hasvar stock* spill*, local(stockspill)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_4

	*5) HSW and MSW | F + IY + CY
	ppmlhdfe F2.${depvar}_${ttt} MSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	hasvar stock* spill*, local(stockspill)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_5

	*6) LSW, HSW and MSW | F + IY + CY
	ppmlhdfe F2.${depvar}_${ttt} LSW MSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	hasvar stock* spill*, local(stockspill)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_6

	clonevar GDPPC_foreign = gdppcMPm_shr4_foreign_1995_a	
	clonevar VAEMP_foreign = vaempMPm_shr4_foreign_1995_a
	clonevar GDPgap_foreign = lngdpgap_shr_foreign_1995_a
	clonevar LSW_foreign = lswMPm_shr4_foreign_1995_a
	clonevar HSW_foreign = hswMPm_shr4_foreign_1995_a
	clonevar MSW_foreign = mswMPm_shr4_foreign_1995_a

	*7) LSW and MSW | F + IY + CY | foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign MSW_foreign GDPgap_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	hasvar stock* spill*, local(stockspill)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_7

	*8) MSW and HSW | F + IY + CY | foreign
	ppmlhdfe F2.${depvar}_${ttt} MSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	hasvar stock* spill*, local(stockspill)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_8

	*9) LSW, HSW and MSW | F + IY + CY | foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign MSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	hasvar stock* spill*, local(stockspill)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col_9

	setlabels

	dhoztab * using ${tab_dir}/appendix/Table_A35_middleskillwage.tex, b(%10.2f) se(%10.2f) nogaps replace showdepvarinfo numbers nonotes nolines nomtitles sfmt(a1) scalars("has_stockspill \noalign{\medskip} Stocks and spillovers" "f Firm fixed effects" "iy Industry \stimes year fixed effects" "cy Country \stimes year fixed effects" "obs \noalign{\medskip} Observations"  "firms Number of firms") label mgroups("Domestic and foreign" "Foreign", pattern(1 0 0 0 0 0 1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) drop(*zero _cons stock* spill*) noobs rename(LSW_foreign LSW MSW_foreign MSW HSW_foreign HSW GDPgap_foreign GDPGAP VAEMP_foreign VAEMP GDPPC_foreign GDPPC LSWHSW_foreign LSWHSW) order(LSW MSW HSW GDPGAP VAEMP) ///
		depvar("Auto95") ///
		notes("This table reports the effect of middle-skill wages. All columns include firm and industry-year fixed effects. Columns 4--6 add country-year fixed effects. In Columns 7--9, the macroeconomic variables are the normalized foreign variables as defined in the text. Standard errors are clustered at the firm-level and reported in parentheses.")
	*again, brackets
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close tabl
